<template>
    <div>
    <!-- 第二步的页面，选择参与商家/店铺-->
      <el-tabs type="border-card" >
        <el-tab-pane label="商家">
          <el-row class="search-button-fix t_r">
            <div v-if="this.auth =='0'">
              <el-button
                class="searchButton"
                size="mini"
                @click="searchMerchantFlag = !searchMerchantFlag"
                icon="el-icon-search"
              >搜索</el-button>
              <el-button class="searchButton" size="mini" @click="querySupplierListForAdd()">添加商家</el-button>
              <el-button
                class="resetButton"
                size="mini"
                @click="delAllSelectedSuppliermerchantTableData2()"
              >批量删除</el-button>
            </div>
          </el-row>

          <el-row>
              <div v-if="searchMerchantFlag"  class="f_r mb30">
                <el-form
                  :inline="true"
                  :model="mainMerchantForm"
                  size="mini"
                  ref="mainMerchantForm"
                >
                  <el-form-item label="商家编码：" prop="orgNumber" class="mr30">
                    <el-input v-model="mainMerchantForm.orgNumber" clearable  placeholder="商家编码"></el-input>
                  </el-form-item>
                  <el-form-item label="商家名称：" prop="merchantName">
                    <el-input v-model="mainMerchantForm.merchantName" clearable placeholder="商家名称"></el-input>
                  </el-form-item>
                  <el-form-item label prop="mpName">
                    <el-button
                      class="resetButton"
                      plain
                      size="mini"
                      @click="resetMerchantSearch()"
                    >重置</el-button>
                    <el-button class="resetButton" size="mini" @click="mainMerchantFormFind()">查询</el-button>
                    
                  </el-form-item>
                </el-form>
              </div>
            </el-row>
<!--          <el-row  class="search-button-fix t_r">-->
<!--            <el-button type="primary" size="mini" @click="querySupplierListForAdd()">添加商家</el-button>-->
<!--            <el-button type="primary" size="mini" @click="delAllSelectedSuppliermerchantTableData2()">批量删除</el-button>-->
<!--          </el-row>-->
          <el-table :data="merchantTableData2" v-loading="loading"  style="width: 100%"  @selection-change="merchantTableData2selectionChange"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column prop="orgNumber" label="商家编码">
              <template slot-scope="scope">
                <span>{{ scope.row.orgNumber}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="商家名称">
              <template slot-scope="scope">
                <span>{{ scope.row.name}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="商家类型">
              <template slot-scope="scope">
                <!--<span>{{ scope.row.merchantType}}</span>-->
                <span v-if="scope.row.merchantTypeManager =='1'">自营商家</span>
                <span v-if="scope.row.merchantTypeManager =='2'">第三方商户</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button  type="text" class="textButton" @click="merchantTableData2delete(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--分页栏-->
          <div class="toolbar t_r" >
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              @current-change="mainMerchantRefreshPageRequest"
              :current-page="mainMerchantForm.pageNo"
              :page-size="mainMerchantForm.pageSize"
              :total="mainMerchantTableDataTatol"
              :page-sizes="[10, 20, 30, 40]"
              @size-change="mainMerchantHandleSizeChange"
            ></el-pagination>
          </div>
        </el-tab-pane>


        <el-tab-pane label="店铺">
          <el-row class="search-button-fix t_r">

            <div v-if="this.auth =='0'">
              <el-button
                class="searchButton"
                size="mini"
                @click="searchStoreFlag = !searchStoreFlag"
                icon="el-icon-search"
              >搜索</el-button>
              <el-button class="searchButton" size="mini" @click="querySupplierListForAdd2()">添加店铺</el-button>
              <el-button
                class="resetButton"
                size="mini"
                @click="delAllSelectedSupplierstoreTableData()"
              >批量删除</el-button>
            </div>
          </el-row>

          <el-row>
              <div v-if="searchStoreFlag"  class="f_r mb30">
                <el-form
                  :inline="true"
                  :model="mainMerchantForm"
                  size="mini"
                  ref="mainMerchantForm"
                >
                  <el-form-item label="店铺编码：" prop="storecode" class="mr30">
                    <el-input v-model="mainMerchantForm.storecode" clearable placeholder="店铺编码"></el-input>
                  </el-form-item>
                  <el-form-item label="店铺名称：" prop="storename">
                    <el-input v-model="mainMerchantForm.storename" clearable placeholder="店铺名称"></el-input>
                  </el-form-item>
                  <el-form-item label prop="mpName">
                    <el-button
                      class="resetButton"
                      size="mini"
                      @click="resetStoreSearch()"
                    >重置</el-button>
                    <el-button class="resetButton" size="mini" @click="storeSearch()">查询</el-button>
                    
                  </el-form-item>
                </el-form>
              </div>
            </el-row>


          <!-- <div class="search-button-fix t_r">
            <el-button type="primary" size="mini" @click="querySupplierListForAdd2()">添加店铺</el-button>
            <el-button type="primary" size="mini" @click="delAllSelectedSupplierstoreTableData()">批量删除</el-button>
          </div> -->
          <el-table :data="storeTableData" v-loading="loading" style="width: 100%"  @selection-change="storeTableDataselectionChange">
            <el-table-column type="selection"></el-table-column>
            <el-table-column label="店铺编码">
              <template slot-scope="scope">
                <span>{{ scope.row.storecode}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="店铺名称">
              <template slot-scope="scope">
                <span>{{ scope.row.storename}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="店铺类型">
              <template slot-scope="scope">
                <span v-if="scope.row.storeType =='1'">线上店铺</span>
                <span v-else>线下店铺</span>
              </template>
            </el-table-column>
            <el-table-column  label="所属商家">
              <template slot-scope="scope">
                <span>{{scope.row.merchantName}}</span>
              </template>
            </el-table-column>
            <el-table-column  label="操作">
              <template slot-scope="scope">
                <el-button  type="text" class="textButton"  @click="storeDelete(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--分页栏-->
          <div class="toolbar t_r" >
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              @current-change="mainSroreMerchantRefreshPageRequest"
              :current-page="mainStoreMerchantForm.pageNo"
              :page-size="mainStoreMerchantForm.pageSize"
              :total="storeTableDataTatol"
              :page-sizes="[10, 20, 30, 40]"
              @size-change="mainSroreMerchantHandleSizeChange"
            ></el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>

    <!-- 第二步的浮框，选择参与商家 -->
    <el-dialog :title="'添加商家'" :visible.sync="editDialogVisible" :close-on-click-modal="false" >
      <div>
        <div class="flex">
          <el-form :inline="true" :model="merchantForm" size="mini" ref="merchantForm" class="mb30">
            <el-form-item label="商家编号："  prop="orgNumber" class="w178">
              <el-input v-model="merchantForm.orgNumber" placeholder="商家编号" maxlength="80" size="mini" clearable ></el-input>
            </el-form-item>
            <el-form-item label="商家名称："   prop="name" class="w178">
              <el-input v-model="merchantForm.name" placeholder="商家名称" maxlength="80" size="mini" clearable ></el-input>
            </el-form-item>
            <el-form-item label="商家类型：" prop="merchantType" class="w178">
              <el-select v-model="merchantForm.merchantType" placeholder="全部" clearable>
<!--                <el-option label="全部" value=""></el-option>-->
                <el-option label="自营商家" value="1"></el-option>
                <el-option label="第三方商户" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div class="t_r mb30 w100">
            <el-button  size="mini" @click="resetMerchantForm" class="resetButton" icon="el-icon-refresh">重置</el-button>
            <el-button  size="mini" @click="merchantFormFind()" class="searchButton" icon="el-icon-search">搜索</el-button>
            
          </div>
        </div>
        <div >
          <el-table :data="merchantTableData" v-loading="loading"  style="width: 100%" @selection-change="addSelectionChange">
            <el-table-column type="selection"></el-table-column>
            <el-table-column align="center" label="商家编号">
              <template slot-scope="scope">
                <span>{{ scope.row.orgNumber}}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="商家名称">
              <template slot-scope="scope">
                <span>{{ scope.row.name}}</span>
              </template>
            </el-table-column>
            <el-table-column width="300px" align="center" label="商家类型">
              <template slot-scope="scope">
                <span v-if="scope.row.merchantType =='1'">自营商家</span>
                  <span v-if="scope.row.merchantType =='2'">第三方商户</span>
              </template>
            </el-table-column>
          </el-table>
          <!--分页栏-->
          <div class="toolbar t_r">
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              @current-change="refreshPageRequest"
              :current-page="merchantForm.start"
              :page-size="merchantForm.limit"
              :total="merchantTableDataTatol"
              :page-sizes="[10, 20, 30, 40]"
              @size-change="handleSizeChange"
            ></el-pagination>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" >
        <el-button  :size="size" @click.native="editDialogVisible = false" class="cancelButton">取消</el-button>
        <el-button :size="size"  class="searchButton"  @click.native="editDialogVisible = false" @click="merchantSaveBatch">确定</el-button>
      </div>
    </el-dialog>

    <!-- 第二步的浮框，选择参与店铺 -->
    <el-dialog :title="'添加参与店铺'"  :visible.sync="editDialogVisible2" :close-on-click-modal="false" >
      <div>
        <div>
          <el-form :inline="true" :model="storeForm" size="mini" ref="storeForm">
            <el-form-item label="店铺名称：" label-width="112px" class="t_r m_b30 w178" prop="storeName">
              <el-input v-model="storeForm.storeName" placeholder="店铺名称" maxlength="80" size="mini" clearable ></el-input>
            </el-form-item>
            <el-form-item label="店铺编码：" class="w178" prop="storeCode">
              <el-input v-model="storeForm.storeCode" clearable placeholder="店铺编码" maxlength="80"></el-input>
            </el-form-item>
            <el-form-item label="所属商家名称：" prop="merchantName" class="w178">
              <el-input v-model="storeForm.merchantName" placeholder="所属商家名称" maxlength="80" size="mini" clearable ></el-input>
            </el-form-item>
            <el-form-item label="店铺类型：" prop="storeType" class="w178" label-width="112px" >
              <el-select v-model="storeForm.storeType"  clearable placeholder="全部">
<!--                <el-option label="全部" value=""></el-option>-->
                <el-option label="自营店铺" value="0"></el-option>
                <el-option label="入驻店铺" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-row class="mb30 t_r  mt20">
            <el-button  class="resetButton" icon="el-icon-refresh" size="mini" @click="resetStoreForm">重置</el-button>
            <el-button  class="searchButton" size="mini" @click="storeFormFind()" icon="el-icon-search">搜索</el-button>
            
          </el-row>
        </div>
        <div >
          <el-table :data="storeTableData2"  style="width: 100%" @selection-change="storeTableData2AddSelectionChange">
            <el-table-column type="selection"></el-table-column>
            <el-table-column prop="id" label="店铺编号" width="320px">
              <template slot-scope="scope">
                <span>{{ scope.row.storeCode}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="productName" label="店铺名称">
              <template slot-scope="scope">
                <span>{{ scope.row.storeName}}</span>
              </template>
            </el-table-column>
             <el-table-column prop="storeType" label="店铺类型">
              <template slot-scope="scope">
                <span v-if="scope.row.storeType == '0'">自营店铺</span>
                <span v-if="scope.row.storeType == '1'">入驻店铺</span>
              </template>
            </el-table-column>
            <el-table-column prop="productName" label="所属商家">
              <template slot-scope="scope">
                <span>{{ scope.row.merchantName}}</span>
              </template>
            </el-table-column>
          </el-table>
          <!--分页栏-->
          <div class="toolbar t_r" >
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              @current-change="refreshPageRequestStore"
              :current-page="merchantFormStore.start"
              :page-size="merchantFormStore.limit"
              :total="merchantTableDataTatolStore"
              style="float:right;"
              :page-sizes="[10, 20, 30, 40]"
              @size-change="handleSizeChangeStore"
            ></el-pagination>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button class="cancelButton" :size="size" @click.native="editDialogVisible2 = false">取消</el-button>
        <el-button :size="size"  class="searchButton"  @click.native="editDialogVisible2 = false" @click="storeSaveBatch">确定</el-button>
      </div>
    </el-dialog>

  </div>

</template>
<script>
  import KtButton from "@/views/Core/KtButton";

  export default {
    name: "Tab",
    components: {
      KtButton
    },
    data() {
      return {
        // --------------------------公共数据----------------------------------
        promotionId:null,
        size:'small',
        loading:false,
        editDialogVisible:false,  //商家页面显示
        editDialogVisible2:false, //店铺页面显示

        // --------------------------第二步数据----------------------------------
        // merchantSearchForm: {
        //   merchantName: null,
        //   orgNumber:null,
        //   promotionId: this.promotionId,
        //   merchantType: "1",
        //   pageNo: 1,
        //   pageSize: 10,
        //   orderBys: {
        //     f: "create_time",
        //     s: "desc"
        //   }
        // },
        promotion:{
          //促销id
          id:"",
          promotionId:"1",
          merchantType:"1",
        },
        pomotionListObject:{pomotionList:[]},
        mainMerchantForm:{
          merchantName: null,
          orgNumber:null,
          // merchantCode:null,
          storename:"",
          storeid:"",
          promotionId: this.promotionId,
          merchantType:"1",
          pageNo:1,
          pageSize:10,
          orderBys:{
            "f":"create_time",
            "s":"desc"
          }
        },
        // storeSearchForm: {
        //   promotionId: this.promotionId,
        //   merchantType: "2",
        //   pageNo: 1,
        //   pageSize: 10,
        //   orderBys: {
        //     f: "create_time",
        //     s: "desc"
        //   }
        // },
        mainMerchantTableDataTatol: 0,
        //---------------------------商家-------start
        auth:0,
        searchStoreFlag: false,
        searchMerchantFlag:false,
        merchantForm: {
          start:1,
          limit:10,
          name:"",
          orgNumber:"",
        },
        merchantFormStore: {
          start:1,
          limit:10,
        },
        merchantTableDataTatol: 0,
        merchantTableDataTatolStore: 0,
        merchantTableData: [
        ],
        //选择的商家列表
        merchantTableData2: [
        ],
        //---------------------------商家------------end
        storeForm:{
          start:1,
          limit:10,
          merchantIds: [],
          storeName: null,
          storeCode: null,
          orgNumber: null,
          merchantName: null,
        },

        mainStoreMerchantForm:{
          promotionId: this.promotionId,
          storeId:"",
          storeName:"",
          merchantType:"2",
          pageNo:1,
          pageSize:10,
          orderBys:{
            "f":"create_time",
            "s":"desc"
          }
        },
        storeTableDataTatol: 0,
        storeTableData:[
        ],
        //选择店铺
        storeTableData2Tatol: 0,
        storeTableData2: [],
        //选择店铺
        addSelectionStoreTableData2: [],
        merchantTableData2Selection:[],
        merchantTableData2SelectionIndex:0,
        storeTableDataSelection:[],
        storeTableDataSelectionIndex:0,
        addSelection: [],

        deptType:localStorage.getItem("deptType"),//当前登录用户所属组织类型
        channelCodes:[],//所选子平台

      };
    },
    methods: {
      init(promotionId, auth){
        this.promotionId = promotionId;
        this.promotion.promotionId = promotionId;
        this.mainMerchantForm.promotionId = promotionId;
        this.mainStoreMerchantForm.promotionId = promotionId;
        this.mainMerchantFormFind();
        this.mainStoreTableDataFormFind();
        if(this.deptType == 0){
          //获取促销基本信息详情,为了获取到childIds
          this.getPromotionDetail();
        }
      },


      resetMerchantSearch() {
        //需要校验的keys
        let keys = ["merchantName","orgNumber"]
        //调用校验
        let  boo = this.seachTableUtils.checkFromIsNull(this.mainMerchantForm,keys)
        if(!boo){
          return;
        }

        this.resetForm("mainMerchantForm");
        this.mainMerchantFormFind();
      },
      //---------------------------------第二步---------------------------------------
      //添加商家
      querySupplierListForAdd() {
        this.merchantFormFind();
        this.editDialogVisible = true;
        this.promotion.merchantType=1;
      },
      //添加店铺
      querySupplierListForAdd2() {
        this.storeFormFind();
        this.editDialogVisible2 = true;
        this.promotion.merchantType=2;
      },
      delAllSelectedSupplierstoreTableData(){
        if (this.storeTableDataSelection.length == 0){
          this.$message({message: "未选择删除条目", type: "error"});
          return;
        }
        this.$confirm("确认批量删除已选内容？", "提示", {
          type: "warning",
          closeOnClickModal: false,
        })
          .then(() => {
            let ids = this.storeTableDataSelection.map(item => item.id).toString();
            let idArray = (ids + "").split(",");
            this.delAllSelectedSupplierstoreTableDataQueryList(idArray);
          })
          .catch(() => {});
      },
      delAllSelectedSupplierstoreTableDataQueryList(idArray){
        let that = this;
        this.promotion.merchantType = "2";
        this.promotion.id = idArray[this.storeTableDataSelectionIndex];
        this.utils.request.promotionMerchantDeleteById(this.promotion, function(){
          that.storeTableDataSelectionIndex++;
          if(that.storeTableDataSelectionIndex<idArray.length){
            that.delAllSelectedSupplierstoreTableDataQueryList(idArray)
          }else{
            //执行成功
            that.storeTableDataSelectionIndex = 0;
            that.$message({message: "删除成功", type: "success"});
            that.mainStoreTableDataFormFind();
            that.mainMerchantFormFind();
            that.mainStoreTableDataFormFind();
          }
        });
      },
      //批量删除商家
      delAllSelectedSuppliermerchantTableData2() {
        if (this.merchantTableData2Selection.length == 0){
          this.$message({message: "未选择删除条目", type: "error"});
          return;
        }
        // this.merchantTableData2Selection;
        this.$confirm("确认批量删除已选内容？", "提示", {
          type: "warning",
          closeOnClickModal: false,
        })
          .then(() => {
            let ids = this.merchantTableData2Selection.map(item => item.id).toString();
            let idArray = (ids + "").split(",");
            this.delAllSelectedSupplierQueryList(idArray);
        })
        .catch(() => {});
      },
      delAllSelectedSupplierQueryList(idArray){
        let that = this;
        this.promotion.merchantType = "1";
        this.promotion.id = idArray[this.merchantTableData2SelectionIndex];
        this.utils.request.promotionMerchantDeleteById(this.promotion, function(){
          that.merchantTableData2SelectionIndex++;
          if(that.merchantTableData2SelectionIndex<idArray.length){
            that.delAllSelectedSupplierQueryList(idArray)
          }else{
            //执行成功
            that.merchantTableData2SelectionIndex = 0;
            that.$message({message: "删除成功", type: "success"});
            that.mainMerchantFormFind();
            that.mainMerchantFormFind();
            that.mainStoreTableDataFormFind();
          }
        });
      },


      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
        }
      },
      //---------------------------店铺------------------------------------------------start

      resetStoreForm(){
        //需要校验的keys
        let keys = ["storeType","merchantName","storeCode","storeName"]
        //调用校验
        let  boo = this.seachTableUtils.checkFromIsNull(this.storeForm,keys)
        if(!boo){
          return;
        }
        this.resetForm("storeForm");
        this.storeFormFind();
      },
      resetStoreSearch() {
        //需要校验的keys
        let keys = ["storename","storecode"]
        //调用校验
        let  boo = this.seachTableUtils.checkFromIsNull(this.mainMerchantForm,keys)
        if(!boo){
          return;
        }
        this.resetForm("mainMerchantForm");
        this.mainStoreTableDataFormFind();
      },
      storeSearch() {
        this.mainStoreTableDataFormFind();
      },
      //查询店铺
      storeFormFind(){
        //商家 取出商家id-->merchantId
        let ids = this.merchantTableData2.map(item => item.merchantId).toString();
        let params = [];
        let idArray = (ids + "").split(",");

        for (var i = 0; i < idArray.length; i++) {
          params.push({id: idArray[i]});
        }

        if(this.merchantTableData2.length==0) {
          this.$message({message: "请先选择商家", type: "success"});
        }

        // this.merchantTableDataTatol;
        this.storeTableData2.splice(0, this.storeTableData2.length);
        var  this__= this;
        this.storeForm.merchantIds = ids;
        this.loading=true;
        this.utils.request.queryByIds(this.storeForm, function (res) {
          if (res.code == "0000") {
            this__.loading=false;
            this__.storeTableData2= res.data.rows;
            this__.merchantTableDataTatolStore = res.data.total;
          }
        });


      },
      storeTableDataselectionChange(val){
        this.storeTableDataSelection=val;
      },
      merchantTableData2selectionChange(val){
        this.merchantTableData2Selection=val;
      },
      //选择店铺
      storeTableData2AddSelectionChange(val){
        this.addSelectionStoreTableData2=val;
      },

      //批量添加店铺
      storeSaveBatch(){
        this.pomotionListObject.pomotionList.splice(0,this.pomotionListObject.pomotionList.length);

        for (var i = 0; i < this.addSelectionStoreTableData2.length; i++) {
          var temp = {};

          temp.promotionId         = this.promotionId;
          temp.storeId           =  this.addSelectionStoreTableData2[i].id;
          temp.merchantName = this.addSelectionStoreTableData2[i].merchantName
          temp.merchantType        = "2";
          temp.storeName        = this.addSelectionStoreTableData2[i].storeName;
          temp.storeCode =this.addSelectionStoreTableData2[i].storeCode;
          temp.storeType           =this.addSelectionStoreTableData2[i].storeType;
          temp.merchantId         = this.addSelectionStoreTableData2[i].merchantId;
          this.pomotionListObject.pomotionList.push(temp);
        }

        let callback = res => {
          if (res.code == "0000") {
            this.mainStoreTableDataFormFind();
          }
        };

        this.utils.request.promotionMerchantSaveBatch(this.pomotionListObject, callback );

      },
      //查询绑定关系的店铺
      mainStoreTableDataFormFind(){
        // this.storeTableData
        this.mainMerchantForm.merchantType="2";
        this.mainMerchantForm.promotionId=this.promotionId;
        var  this__= this;
        this__.loading = true;
        this.utils.request.promotionMerchantList(this.mainMerchantForm, function (res) {
          if (res.code == "0000") {
            this__.loading = false;
            //店铺调试
            this__.storeTableData = res.data.list;
            this__.storeTableDataTatol = res.data.totalCount;
          }
        });
      },


      // 换页刷新
      mainSroreMerchantRefreshPageRequest: function (pageNum) {
        this.mainStoreMerchantForm.pageNo = pageNum;
        this.mainStoreTableDataFormFind();
      },
      // 换页刷新
      mainSroreMerchantHandleSizeChange: function (pageSize) {
        this.mainStoreMerchantForm.pageNo = 1;
        this.mainStoreMerchantForm.pageSize = pageSize;
        this.mainStoreTableDataFormFind();
      },


      // 换页刷新
      refreshPageRequestStore: function (pageNum) {
        this.merchantFormStore.start = pageNum;
        // this.merchantFormFind();
        this.storeFormFind();
      },
      // 换页刷新
      handleSizeChangeStore: function (pageSize) {
        this.merchantForm;
        this.merchantFormStore.limit = pageSize;
        this.merchantFormStore.start = 1;
        this.storeFormFind();
      },

      //---------------------------分页------------------------------------------------start
      // 换页刷新
      refreshPageRequest: function (pageNum) {
        this.merchantForm.start = pageNum;
        this.merchantFormFind();
      },
      // 换页刷新
      handleSizeChange: function (pageSize) {
        this.merchantForm.limit = pageSize;
        this.merchantForm.start = 1;
        this.merchantFormFind();
      },

      // 换页刷新
      mainMerchantRefreshPageRequest: function (pageNum) {
        this.mainMerchantForm.pageNo = pageNum;
        this.mainMerchantFormFind();
      },
      // 换页刷新
      mainMerchantHandleSizeChange: function (pageSize) {
        this.mainMerchantForm.pageNo = 1;
        this.mainMerchantForm.pageSize = pageSize;
        this.mainMerchantFormFind();
      },
      //查询绑定关系的商家
      mainMerchantFormFind(){
        this.mainMerchantForm.merchantType="1";
        var  this__= this;
        this__.loading = true;
        this.mainMerchantForm.promotionId=this.promotionId;
        this.utils.request.promotionMerchantList(this.mainMerchantForm, function (res) {

          if (res.code == "0000") {
            this__.loading = false;
            var ret = [];
            for (var i = 0; i < res.data.list.length; i++) {
              var temp = {};
              temp.id = res.data.list[i].id;
              temp.name = res.data.list[i].merchantName;
              temp.merchantCode =  res.data.list[i].merchantCode;
              temp.orgNumber =  res.data.list[i].orgNumber;
              temp.merchantTypeManager = res.data.list[i].merchantTypeManager;
              temp.merchantId=res.data.list[i].merchantId;

              ret.push(temp);
            }

            this__.merchantTableData2 = ret;
            this__.mainMerchantTableDataTatol = res.data.total;
          }
        });
      },
      // 删除店铺
      storeDelete(id){

        let callback = res => {
          this.mainStoreTableDataFormFind();

          if (res.code == "0000" && res.data=="1") {
            //删除成功再调分页
            this.$message({message: "删除成功", type: "success"});
            this.mainMerchantFormFind();
            this.mainStoreTableDataFormFind();
          }else{
            this.$message({message: "操作失败, " + res.msg, type: "error"});
          }
        };

        this.promotion.id = id;
        this.utils.request.promotionMerchantDeleteById(this.promotion,callback);
      },

      //---------------------------分页------------------------------------------------end
      //删除商家
      merchantTableData2delete(id){

        let callback = res => {
          this.mainMerchantFormFind();
          if (res.code == "0000" && res.data=="1") {
            //删除成功再调分页
            this.$message({message: "删除成功", type: "success"});
            this.mainMerchantFormFind();
            this.mainStoreTableDataFormFind();
          }else{
            this.$message({message: "操作失败, " + res.msg, type: "error"});
          }
        };

        this.promotion.id = id;
        this.utils.request.promotionMerchantDeleteById(this.promotion,callback);
      },
      //选择商家
      addSelectionChange(val) {
        this.addSelection = val;
      },

      //批量添加商家
      merchantSaveBatch(){
        this.pomotionListObject.pomotionList.splice(0,this.pomotionListObject.pomotionList.length);

        for (var i = 0; i < this.addSelection.length; i++) {
          var temp = {};

          temp.promotionId = this.promotionId;
          temp.merchantId =  this.addSelection[i].id;
          temp.merchantType = "1";
          temp.merchantName = this.addSelection[i].name;
          temp.merchantTypeManager=this.addSelection[i].merchantType;
          temp.orgNumber=this.addSelection[i].orgNumber;
          temp.name = this.addSelection[i].name;

          this.pomotionListObject.pomotionList.push(temp);
        }

        let callback = res => {
          if (res.code == "0000") {
            this.mainMerchantForm.merchantType="1";
            this.mainMerchantFormFind();
          }
        };

        var  this__= this;
        this.utils.request.promotionMerchantSaveBatch(this.pomotionListObject, callback );
      },

      resetMerchantForm(){
        //需要校验的keys
        let keys = ["merchantType","name","orgNumber"]
        //调用校验
        let  boo = this.seachTableUtils.checkFromIsNull(this.merchantForm,keys)
        if(!boo){
          return;
        }
        this.resetForm('merchantForm');
        this.merchantFormFind();
      },
      getPromotionDetail(){
          let params = {"id": this.promotionId},
            _this = this;
          this.utils.request.promotionDetailById(params, res=>{
            if (res.code == "0000") {
              let channelCodesStr = res.data.channelCodes;
              if(null != channelCodesStr && channelCodesStr != ''){
                _this.channelCodes = channelCodesStr.split(",");
              }
            }
          });
      },
      //查询商家
      merchantFormFind(){
        var  this__= this;
        this__.loading = true;
        //获取促销基本信息详情,为了获取到childIds
        if(this.deptType == 0 && this__.channelCodes && this__.channelCodes.length >0){
          this__.merchantForm.childIds = this__.channelCodes;
        }
        this.utils.request.queryMerchantList(this.merchantForm, function (res) {
          // this.utils.request.queryMerchantListOther(null, function (res) {
          if (res.code == "0000") {
            this__.loading = false;
            this__.merchantTableData= res.data.rows;
            this__.merchantTableDataTatol = res.data.total;
          }
        });
      }
    }

  };
</script>

<style scoped lang="scss">
  .t_r{
    text-align:right;
  }
  .m_b30{
    margin-bottom: 30px !important;
  }
  .w178{
    /deep/ .el-input__inner{
      width: 178px !important;
    }
  }
</style>
